package com.java.team.view;

import com.java.team.domain.Employee;
import com.java.team.domain.Programmer;
import com.java.team.service.NameListService;
import com.java.team.service.TeamException;
import com.java.team.service.TeamService;

public class TeamView {
    private NameListService listsvc = new NameListService();
    private TeamService teamSvc = new TeamService();

    public void enterMainMenu(){
        boolean whileFlag = true;
        char choice = 0;
        while (whileFlag){
            System.out.println("-------------------------------开发团队调度软件--------------------------------\n");
            if(choice != '1'){
                listAllEmployees();
            }
            System.out.println("-------------------------------------------------------------------------------");
            System.out.print("1-团队列表  2-添加团队成员  3-删除团队成员 4-退出   请选择(1-4)：");
            choice = TSUtility.readMenuSelection();
            switch (choice){
                case '1':
                    getTeam();
                    break;
                case '2':
                    addMember();
                    break;
                case '3':
                    deleteMember();
                    break;
                case '4':
                    System.out.print("确认是否退出(Y/N)：");
                    char isExit = TSUtility.readConfirmSelection();
                    if(isExit == 'Y'){
                        whileFlag = false;
                    }
                    break;
            }
        }
    }

    /**
     * 以表格形式列出公司所有成员
     */
    private void listAllEmployees(){
        System.out.println("ID\t姓名\t年龄\t工资\t职位\t状态\t奖金\t股票\t领用设备");
        Employee[] allEmployees = listsvc.getAllEmployees();
        for (int i = 0; i < allEmployees.length; i++) {
            System.out.println(allEmployees[i]);
        }
    }

    /**
     * 显示团队成员列表操作
     */
    private void getTeam(){
        Programmer[] team = teamSvc.getTeam();
        if(team == null || team.length == 0){
            System.out.println("开发团队目前没有成员！");
        }else{
            System.out.println("TID/ID\t姓名\t年龄\t工资\t职位\t奖金\t股票\n");
            for (int i = 0; i < team.length; i++) {
                System.out.println(team[i].getDetailsForTeam());
            }
        }
    }

    /**
     * 实现添加成员操作
     */
    private void addMember(){
//        System.out.println("实现添加成员操作");
        System.out.print("请输入要添加的员工ID：");
        int id = TSUtility.readInt();
        try {
            Employee employee = listsvc.getEmployee(id);
            teamSvc.addMember(employee);
            System.out.println("添加成功");
        } catch (TeamException e) {
            System.out.println("添加失败，原因："+e.getMessage());
        }
        TSUtility.readReturn();
    }

    /**
     * 实现删除成员操作
     */
    private void deleteMember(){
//        System.out.println("实现删除成员操作");
        System.out.print("请输入要删除员工的TID：");
        int memberId = TSUtility.readInt();
        try {
            System.out.print("确认是否删除(Y/N)：");
            char isDelete = TSUtility.readConfirmSelection();
            if(isDelete == 'Y'){
                teamSvc.removeMember(memberId);
                System.out.println("删除成功");
            }
        } catch (TeamException e) {
            System.out.println("删除失败，原因："+e.getMessage());
        }
        TSUtility.readReturn();
    }

    public static void main(String[] args) {
        TeamView teamView = new TeamView();
        teamView.enterMainMenu();
    }
}
